package com.linktel.productionLine.service;

import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.dao.BaseStepDao;
import com.linktel.productionLine.dao.ProcedureProductSeriesStepDao;
import com.linktel.productionLine.entity.BaseStep;
import com.linktel.productionLine.entity.ProcedureProductSeriesStep;
import com.linktel.productionLine.entity.ProductionLine;
import com.linktel.productionLine.entity.dto.displayStep.DisplayStepDTO;
import com.linktel.productionLine.entity.dto.userProductionLine.RelationDTO;
import com.linktel.productionLine.entity.dto.commitUser.CommitUserDTO;
import com.linktel.productionLine.entity.query.displayStep.AddDisplayStepQuery;
import com.linktel.productionLine.entity.query.displayStep.EditDisplayStepQuery;
import com.linktel.productionLine.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author HuangJiaHui
 * @Date 2023/9/19 10:09
 */
@Service
@AllArgsConstructor
public class DisplayStepService {
    private ProcedureProductSeriesStepDao procedureProductSeriesStepDao;
    private CommitUserProductionLineService commitUserProductionLineService;
    private CommitUserService commitUserService;
    private ProductionLineService productionLineService;
    private BaseStepDao baseStepDao;

    /**
     * 查询当前用户管理的产线和它包含的工序
     *
     * @param currentUserId 当前用户id
     * @return 当前用户管理的产线和它包含的工序列表
     */
    public List<DisplayStepDTO> listDisplayStep(Long currentUserId) {
        List<String> productionCodeList = commitUserProductionLineService.listRelationByUserId(currentUserId).stream().map(RelationDTO::getProductionLineCode).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(productionCodeList)) {
            return null;
        }
        return procedureProductSeriesStepDao.listDisplayStep(productionCodeList);
    }

    /**
     * 新增产线和它包含的工序
     *
     * @param addDisplayStepQuery 产线和它包含的工序新增对象的封装
     * @return 是否新增成功
     */
    public Boolean addDisplayStep(AddDisplayStepQuery addDisplayStepQuery) {
        String workLineCode = addDisplayStepQuery.getWorkLineCode();
        Long currentUserId = addDisplayStepQuery.getCurrentUserId();
        if (workLineCode == null || "".equals(workLineCode)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "产线编码不能为空");
        }
        // 只能有一条产线数据
        Long id = procedureProductSeriesStepDao.getByWorkLineCode(workLineCode);
        if (id != null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "一条产线只能有一条数据");
        }
        ProcedureProductSeriesStep procedureProductSeriesStep = getProcedureProductSeriesStep(addDisplayStepQuery);

        CommitUserDTO commitUserDTO = commitUserService.getById(currentUserId);
        procedureProductSeriesStep.setPqe(commitUserDTO.getName());

        ProductionLine productionLine = productionLineService.getByCode(workLineCode);
        procedureProductSeriesStep.setWorkLineId(productionLine.getId());
        procedureProductSeriesStep.setWorkLineCode(productionLine.getCode());
        procedureProductSeriesStep.setWorkLineName(productionLine.getName());

        return procedureProductSeriesStepDao.addDisplayStep(procedureProductSeriesStep);

    }

    /**
     * 将12个工序，不为空的放到最前面
     *
     * @param addDisplayStepQuery 包含12道工序的数据
     * @return 排好序的12道工序
     */
    private ProcedureProductSeriesStep getProcedureProductSeriesStep(AddDisplayStepQuery addDisplayStepQuery) {
        List<String> stepCodeList = new ArrayList<>();
        stepCodeList.add(addDisplayStepQuery.getStep1Code());
        stepCodeList.add(addDisplayStepQuery.getStep2Code());
        stepCodeList.add(addDisplayStepQuery.getStep3Code());
        stepCodeList.add(addDisplayStepQuery.getStep4Code());
        stepCodeList.add(addDisplayStepQuery.getStep5Code());
        stepCodeList.add(addDisplayStepQuery.getStep6Code());
        stepCodeList.add(addDisplayStepQuery.getStep7Code());
        stepCodeList.add(addDisplayStepQuery.getStep8Code());
        stepCodeList.add(addDisplayStepQuery.getStep9Code());
        stepCodeList.add(addDisplayStepQuery.getStep10Code());
        stepCodeList.add(addDisplayStepQuery.getStep11Code());
        stepCodeList.add(addDisplayStepQuery.getStep12Code());
        ProcedureProductSeriesStep procedureProductSeriesStep = new ProcedureProductSeriesStep();
        stepCodeList = stepCodeList.stream().filter(item -> item != null && !"".equals(item)).collect(Collectors.toList());
        int size = stepCodeList.size();
        if (size > 0) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(0));
            procedureProductSeriesStep.setStep1Code(baseStep.getCode());
            procedureProductSeriesStep.setStep1Name(baseStep.getName());
            procedureProductSeriesStep.setStep1Id(baseStep.getId());
        }
        if (size > 1) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(1));
            procedureProductSeriesStep.setStep2Code(baseStep.getCode());
            procedureProductSeriesStep.setStep2Name(baseStep.getName());
            procedureProductSeriesStep.setStep2Id(baseStep.getId());
        }
        if (size > 2) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(2));
            procedureProductSeriesStep.setStep3Code(baseStep.getCode());
            procedureProductSeriesStep.setStep3Name(baseStep.getName());
            procedureProductSeriesStep.setStep3Id(baseStep.getId());
        }
        if (size > 3) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(3));
            procedureProductSeriesStep.setStep4Code(baseStep.getCode());
            procedureProductSeriesStep.setStep4Name(baseStep.getName());
            procedureProductSeriesStep.setStep4Id(baseStep.getId());
        }
        if (size > 4) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(4));
            procedureProductSeriesStep.setStep5Code(baseStep.getCode());
            procedureProductSeriesStep.setStep5Name(baseStep.getName());
            procedureProductSeriesStep.setStep5Id(baseStep.getId());
        }
        if (size > 5) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(5));
            procedureProductSeriesStep.setStep6Code(baseStep.getCode());
            procedureProductSeriesStep.setStep6Name(baseStep.getName());
            procedureProductSeriesStep.setStep6Id(baseStep.getId());
        }
        if (size > 6) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(6));
            procedureProductSeriesStep.setStep7Code(baseStep.getCode());
            procedureProductSeriesStep.setStep7Name(baseStep.getName());
            procedureProductSeriesStep.setStep7Id(baseStep.getId());
        }
        if (size > 7) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(7));
            procedureProductSeriesStep.setStep8Code(baseStep.getCode());
            procedureProductSeriesStep.setStep8Name(baseStep.getName());
            procedureProductSeriesStep.setStep8Id(baseStep.getId());
        }
        if (size > 8) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(8));
            procedureProductSeriesStep.setStep9Code(baseStep.getCode());
            procedureProductSeriesStep.setStep9Name(baseStep.getName());
            procedureProductSeriesStep.setStep9Id(baseStep.getId());
        }
        if (size > 9) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(9));
            procedureProductSeriesStep.setStep10Code(baseStep.getCode());
            procedureProductSeriesStep.setStep10Name(baseStep.getName());
            procedureProductSeriesStep.setStep10Id(baseStep.getId());
        }
        if (size > 10) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(10));
            procedureProductSeriesStep.setStep11Code(baseStep.getCode());
            procedureProductSeriesStep.setStep11Name(baseStep.getName());
            procedureProductSeriesStep.setStep11Id(baseStep.getId());
        }
        if (size > 11) {
            BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCodeList.get(11));
            procedureProductSeriesStep.setStep12Code(baseStep.getCode());
            procedureProductSeriesStep.setStep12Name(baseStep.getName());
            procedureProductSeriesStep.setStep12Id(baseStep.getId());
        }
        return procedureProductSeriesStep;
    }

    /**
     * 根据产线id删除产线工序id
     *
     * @param workLineCode 产线id
     * @return 是否删除成功
     */
    public Boolean deleteDisplayStep(String workLineCode) {
        return procedureProductSeriesStepDao.deleteDisplayStep(workLineCode);
    }

    /**
     * 修改 产线显示工序
     *
     * @param editDisplayStepQuery 修改的新的书v就
     * @return 是否修改成功
     */
    public Boolean editDisplayStep(EditDisplayStepQuery editDisplayStepQuery) {
        String workLineCode = editDisplayStepQuery.getWorkLineCode();
        if (workLineCode == null || "".equals(workLineCode)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "产线编码不能为空");
        }
        AddDisplayStepQuery addDisplayStepQuery = new AddDisplayStepQuery();
        BeanUtils.copyProperties(editDisplayStepQuery, addDisplayStepQuery);
        ProcedureProductSeriesStep procedureProductSeriesStep = getProcedureProductSeriesStep(addDisplayStepQuery);
        procedureProductSeriesStep.setWorkLineCode(workLineCode);
        return procedureProductSeriesStepDao.editDisplayStep(procedureProductSeriesStep);
    }
}
